package com.baor.controller;
import com.baor.util.R;
import com.baor.domain.DeviceInfo;
import com.baor.service.GgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; // 新增：导入文件上传类

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/gg")
public class GgController {
    @Autowired
    private GgService ggService;

    @GetMapping("/list")
    public R getAllDevices() {
        return R.ok( ggService.gglist());
    }


    @PutMapping("/update")

    public R updateDevice(@RequestPart("deviceInfo") DeviceInfo deviceInfo,
                          @RequestPart(value = "imageFiles", required = false) MultipartFile[] imageFiles) {
        boolean success = ggService.updateDevice(deviceInfo, imageFiles);
        if (success) {
            return R.ok();
        } else {
            return R.er();
        }
    }


    @PostMapping("/insert")

    public R insertSomething(@RequestPart("deviceInfo") DeviceInfo deviceInfo,
                             @RequestPart(value = "imageFiles", required = false) MultipartFile[] imageFiles) {
        int row = ggService.insert(deviceInfo, imageFiles);
        if(row > 0){
            return R.ok();
        } else {
            if(row == -1){
                return R.er("已经包含该ID");
            } else if(row == -2){
                return R.er("图片上传失败");
            } else {
                return R.er();
            }
        }
    }
}